package com.youyi.yychosesdk.utils.file;

import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.util.Log;
import android.webkit.MimeTypeMap;
import com.youyi.yychosesdk.constant.Type;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;

public class FileUtils {
    private static final boolean DEBUG = false;
    public static final String DOCUMENTS_DIR = "documents";
    public static final String HIDDEN_PREFIX = ".";
    static final String TAG = "FileUtils";

    private static void logDir(File file) {
    }

    private FileUtils() {
    }

    public static String getExtension(String str) {
        if (str == null) {
            return null;
        }
        int lastIndexOf = str.lastIndexOf(HIDDEN_PREFIX);
        return lastIndexOf >= 0 ? str.substring(lastIndexOf) : "";
    }

    public static boolean isLocal(String str) {
        return str != null && !str.startsWith("http://") && !str.startsWith("https://");
    }

    public static boolean isMediaUri(Uri uri) {
        return "media".equalsIgnoreCase(uri.getAuthority());
    }

    public static Uri getUri(File file) {
        if (file != null) {
            return Uri.fromFile(file);
        }
        return null;
    }

    public static File getPathWithoutFilename(File file) {
        if (file == null) {
            return null;
        }
        if (file.isDirectory()) {
            return file;
        }
        String name = file.getName();
        String absolutePath = file.getAbsolutePath();
        String substring = absolutePath.substring(0, absolutePath.length() - name.length());
        if (substring.endsWith("/")) {
            substring = substring.substring(0, substring.length() - 1);
        }
        return new File(substring);
    }

    public static String getMimeType(File file) {
        String extension = getExtension(file.getName());
        return extension.length() > 0 ? MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.substring(1)) : "application/octet-stream";
    }

    public static String getMimeType(Context context, Uri uri) {
        return getMimeType(new File(getPath(context, uri)));
    }

    public static String getMimeType(Context context, String str) {
        String type = context.getContentResolver().getType(Uri.parse(str));
        return type == null ? "application/octet-stream" : type;
    }

    public static boolean isLocalStorageDocument(Context context, Uri uri) {
        return (context.getApplicationContext().getPackageName() + ".fileprovider").equals(uri.getAuthority());
    }

    public static boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }

    public static boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }

    public static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }

    public static boolean isGooglePhotosUri(Uri uri) {
        return "com.google.android.apps.photos.content".equals(uri.getAuthority());
    }

    /* JADX WARNING: Code restructure failed: missing block: B:12:0x002b, code lost:
        if (r8 != null) goto L_0x0037;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:18:0x0035, code lost:
        if (r8 != null) goto L_0x0037;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:19:0x0037, code lost:
        r8.close();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:20:0x003a, code lost:
        return null;
     */
    /* JADX WARNING: Removed duplicated region for block: B:24:0x003f  */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static String getDataColumn(Context r8, Uri r9, String r10, String[] r11) {
        /*
            java.lang.String r0 = "_data"
            java.lang.String[] r3 = new java.lang.String[]{r0}
            r7 = 0
            android.content.ContentResolver r1 = r8.getContentResolver()     // Catch:{ Exception -> 0x0030, all -> 0x002e }
            r6 = 0
            r2 = r9
            r4 = r10
            r5 = r11
            android.database.Cursor r8 = r1.query(r2, r3, r4, r5, r6)     // Catch:{ Exception -> 0x0030, all -> 0x002e }
            if (r8 == 0) goto L_0x002b
            boolean r9 = r8.moveToFirst()     // Catch:{ Exception -> 0x0029 }
            if (r9 == 0) goto L_0x002b
            int r9 = r8.getColumnIndexOrThrow(r0)     // Catch:{ Exception -> 0x0029 }
            java.lang.String r9 = r8.getString(r9)     // Catch:{ Exception -> 0x0029 }
            if (r8 == 0) goto L_0x0028
            r8.close()
        L_0x0028:
            return r9
        L_0x0029:
            r9 = move-exception
            goto L_0x0032
        L_0x002b:
            if (r8 == 0) goto L_0x003a
            goto L_0x0037
        L_0x002e:
            r9 = move-exception
            goto L_0x003d
        L_0x0030:
            r9 = move-exception
            r8 = r7
        L_0x0032:
            r9.printStackTrace()     // Catch:{ all -> 0x003b }
            if (r8 == 0) goto L_0x003a
        L_0x0037:
            r8.close()
        L_0x003a:
            return r7
        L_0x003b:
            r9 = move-exception
            r7 = r8
        L_0x003d:
            if (r7 == 0) goto L_0x0042
            r7.close()
        L_0x0042:
            throw r9
        */
        throw new UnsupportedOperationException("Method not decompiled: com.youyi.yychosesdk.utils.file.FileUtils.getDataColumn(android.content.Context, android.net.Uri, java.lang.String, java.lang.String[]):java.lang.String");
    }

    public static String getPath(Context context, Uri uri) {
        String localPath = getLocalPath(context, uri);
        return localPath != null ? localPath : uri.toString();
    }

    private static String getLocalPath(Context context, Uri uri) {
        Uri uri2 = null;
        if (Build.VERSION.SDK_INT < 19 || !DocumentsContract.isDocumentUri(context, uri)) {
            if ("content".equalsIgnoreCase(uri.getScheme())) {
                if (isGooglePhotosUri(uri)) {
                    return uri.getLastPathSegment();
                }
                return getDataColumn(context, uri, null, null);
            } else if ("file".equalsIgnoreCase(uri.getScheme())) {
                return uri.getPath();
            }
        } else if (isLocalStorageDocument(context, uri)) {
            return DocumentsContract.getDocumentId(uri);
        } else {
            if (isExternalStorageDocument(uri)) {
                String[] split = DocumentsContract.getDocumentId(uri).split(":");
                String str = split[0];
                if ("primary".equalsIgnoreCase(str)) {
                    return Environment.getExternalStorageDirectory() + "/" + split[1];
                } else if ("home".equalsIgnoreCase(str)) {
                    return Environment.getExternalStorageDirectory() + "/documents/" + split[1];
                }
            } else if (isDownloadsDocument(uri)) {
                String documentId = DocumentsContract.getDocumentId(uri);
                if (documentId != null && documentId.startsWith("raw:")) {
                    return documentId.substring(4);
                }
                String[] strArr = {"content://downloads/public_downloads", "content://downloads/my_downloads"};
                for (int i = 0; i < 2; i++) {
                    try {
                        String dataColumn = getDataColumn(context, ContentUris.withAppendedId(Uri.parse(strArr[i]), Long.valueOf(documentId).longValue()), null, null);
                        if (dataColumn != null) {
                            return dataColumn;
                        }
                    } catch (Exception unused) {
                    }
                }
                File generateFileName = generateFileName(getFileName(context, uri), getDocumentCacheDir(context));
                if (generateFileName == null) {
                    return null;
                }
                String absolutePath = generateFileName.getAbsolutePath();
                saveFileFromUri(context, uri, absolutePath);
                return absolutePath;
            } else if (isMediaDocument(uri)) {
                String[] split2 = DocumentsContract.getDocumentId(uri).split(":");
                String str2 = split2[0];
                if ("image".equals(str2)) {
                    uri2 = MediaStore.Images.Media.getContentUri("external");
                } else if (Type.VIDEO.equals(str2)) {
                    uri2 = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else if ("audio".equals(str2)) {
                    uri2 = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                }
                return getDataColumn(context, uri2, "_id=?", new String[]{split2[1]});
            }
        }
        return null;
    }

    public static File getFile(Context context, Uri uri) {
        String path;
        if (uri == null || (path = getPath(context, uri)) == null || !isLocal(path)) {
            return null;
        }
        return new File(path);
    }

    public static String getReadableFileSize(int i) {
        float f;
        DecimalFormat decimalFormat = new DecimalFormat("###.#");
        String str = " KB";
        if (i > 1024) {
            f = (float) (i / 1024);
            if (f > 1024.0f) {
                f /= 1024.0f;
                if (f > 1024.0f) {
                    f /= 1024.0f;
                    str = " GB";
                } else {
                    str = " MB";
                }
            }
        } else {
            f = 0.0f;
        }
        return String.valueOf(decimalFormat.format((double) f) + str);
    }

    public static Intent createGetContentIntent() {
        Intent intent = new Intent("android.intent.action.GET_CONTENT");
        intent.setType("*/*");
        intent.addCategory("android.intent.category.OPENABLE");
        return intent;
    }

    public static Intent getViewIntent(Context context, File file) {
        Uri uriForFile = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileprovider", file);
        Intent intent = new Intent("android.intent.action.VIEW");
        String file2 = file.toString();
        if (file2.contains(".doc") || file2.contains(".docx")) {
            intent.setDataAndType(uriForFile, "application/msword");
        } else if (file2.contains(".pdf")) {
            intent.setDataAndType(uriForFile, "application/pdf");
        } else if (file2.contains(".ppt") || file2.contains(".pptx")) {
            intent.setDataAndType(uriForFile, "application/vnd.ms-powerpoint");
        } else if (file2.contains(".xls") || file2.contains(".xlsx")) {
            intent.setDataAndType(uriForFile, "application/vnd.ms-excel");
        } else if (file2.contains(".zip") || file2.contains(".rar")) {
            intent.setDataAndType(uriForFile, "application/x-wav");
        } else if (file2.contains(".rtf")) {
            intent.setDataAndType(uriForFile, "application/rtf");
        } else if (file2.contains(".wav") || file2.contains(".mp3")) {
            intent.setDataAndType(uriForFile, "audio/x-wav");
        } else if (file2.contains(".gif")) {
            intent.setDataAndType(uriForFile, "image/gif");
        } else if (file2.contains(".jpg") || file2.contains(".jpeg") || file2.contains(".png")) {
            intent.setDataAndType(uriForFile, "image/jpeg");
        } else if (file2.contains(".txt")) {
            intent.setDataAndType(uriForFile, "text/plain");
        } else if (file2.contains(".3gp") || file2.contains(".mpg") || file2.contains(".mpeg") || file2.contains(".mpe") || file2.contains(".mp4") || file2.contains(".avi")) {
            intent.setDataAndType(uriForFile, "video/*");
        } else {
            intent.setDataAndType(uriForFile, "*/*");
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        return intent;
    }

    public static File getDownloadsDir() {
        return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    }

    public static File getDocumentCacheDir(Context context) {
        File file = new File(context.getCacheDir(), DOCUMENTS_DIR);
        if (!file.exists()) {
            file.mkdirs();
        }
        logDir(context.getCacheDir());
        logDir(file);
        return file;
    }

    public static File generateFileName(String str, File file) {
        String str2;
        if (str == null) {
            return null;
        }
        File file2 = new File(file, str);
        if (file2.exists()) {
            int lastIndexOf = str.lastIndexOf(46);
            int i = 0;
            if (lastIndexOf > 0) {
                String substring = str.substring(0, lastIndexOf);
                str2 = str.substring(lastIndexOf);
                str = substring;
            } else {
                str2 = "";
            }
            while (file2.exists()) {
                i++;
                file2 = new File(file, str + '(' + i + ')' + str2);
            }
        }
        try {
            if (!file2.createNewFile()) {
                return null;
            }
            logDir(file);
            return file2;
        } catch (IOException e) {
            Log.w(TAG, e);
            return null;
        }
    }

    /* JADX WARNING: Removed duplicated region for block: B:30:0x0044  */
    /* JADX WARNING: Removed duplicated region for block: B:33:0x004c  */
    /* JADX WARNING: Removed duplicated region for block: B:37:0x0057 A[SYNTHETIC, Splitter:B:37:0x0057] */
    /* JADX WARNING: Removed duplicated region for block: B:42:0x005f A[Catch:{ IOException -> 0x005b }] */
    /* JADX WARNING: Removed duplicated region for block: B:47:? A[RETURN, SYNTHETIC] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private static void saveFileFromUri(Context r3, Uri r4, String r5) {
        /*
        // Method dump skipped, instructions count: 103
        */
        throw new UnsupportedOperationException("Method not decompiled: com.youyi.yychosesdk.utils.file.FileUtils.saveFileFromUri(android.content.Context, android.net.Uri, java.lang.String):void");
    }

    /* JADX WARNING: Code restructure failed: missing block: B:12:0x0020, code lost:
        r1 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:13:0x0021, code lost:
        r2 = null;
        r0 = r1;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:14:0x0024, code lost:
        r4 = th;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:20:0x002f, code lost:
        r2.close();
     */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Removed duplicated region for block: B:14:0x0024 A[ExcHandler: all (th java.lang.Throwable), Splitter:B:1:0x0001] */
    /* JADX WARNING: Removed duplicated region for block: B:20:0x002f  */
    /* JADX WARNING: Removed duplicated region for block: B:25:0x0037 A[SYNTHETIC, Splitter:B:25:0x0037] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static byte[] readBytesFromFile(String r4) {
        /*
            r0 = 0
            java.io.File r1 = new java.io.File     // Catch:{ IOException -> 0x0026, all -> 0x0024 }
            r1.<init>(r4)     // Catch:{ IOException -> 0x0026, all -> 0x0024 }
            long r2 = r1.length()     // Catch:{ IOException -> 0x0026, all -> 0x0024 }
            int r4 = (int) r2     // Catch:{ IOException -> 0x0026, all -> 0x0024 }
            byte[] r4 = new byte[r4]     // Catch:{ IOException -> 0x0026, all -> 0x0024 }
            java.io.FileInputStream r2 = new java.io.FileInputStream     // Catch:{ IOException -> 0x0020, all -> 0x0024 }
            r2.<init>(r1)     // Catch:{ IOException -> 0x0020, all -> 0x0024 }
            r2.read(r4)     // Catch:{ IOException -> 0x001e }
            r2.close()     // Catch:{ IOException -> 0x0019 }
            goto L_0x0032
        L_0x0019:
            r0 = move-exception
            r0.printStackTrace()
            goto L_0x0032
        L_0x001e:
            r0 = move-exception
            goto L_0x002a
        L_0x0020:
            r1 = move-exception
            r2 = r0
            r0 = r1
            goto L_0x002a
        L_0x0024:
            r4 = move-exception
            goto L_0x0035
        L_0x0026:
            r4 = move-exception
            r2 = r0
            r0 = r4
            r4 = r2
        L_0x002a:
            r0.printStackTrace()     // Catch:{ all -> 0x0033 }
            if (r2 == 0) goto L_0x0032
            r2.close()
        L_0x0032:
            return r4
        L_0x0033:
            r4 = move-exception
            r0 = r2
        L_0x0035:
            if (r0 == 0) goto L_0x003f
            r0.close()     // Catch:{ IOException -> 0x003b }
            goto L_0x003f
        L_0x003b:
            r0 = move-exception
            r0.printStackTrace()
        L_0x003f:
            throw r4
        */
        throw new UnsupportedOperationException("Method not decompiled: com.youyi.yychosesdk.utils.file.FileUtils.readBytesFromFile(java.lang.String):byte[]");
    }

    public static File createTempImageFile(Context context, String str) throws IOException {
        return File.createTempFile(str, ".jpg", new File(context.getCacheDir(), DOCUMENTS_DIR));
    }

    public static String getFileName(Context context, Uri uri) {
        if (context.getContentResolver().getType(uri) != null || context == null) {
            Cursor query = context.getContentResolver().query(uri, null, null, null, null);
            if (query == null) {
                return null;
            }
            int columnIndex = query.getColumnIndex("_display_name");
            query.moveToFirst();
            String string = query.getString(columnIndex);
            query.close();
            return string;
        }
        String path = getPath(context, uri);
        if (path == null) {
            return getName(uri.toString());
        }
        return new File(path).getName();
    }

    public static String getName(String str) {
        if (str == null) {
            return null;
        }
        return str.substring(str.lastIndexOf(47) + 1);
    }

    public static String readFileToString(File file) {
        return "1233";
    }
}
